import os

# 将文本写入到somedir/spamspam.txt中
# os.open()和内置函数open()不是同一个函数
# windows下运行报PermissionError
# linux下python2.x print函数参数file报错

dir_fd = os.open('somedir', os.O_RDONLY)

def opener(path, flags):
    return os.open(path, flags, dir_fd=dir_fd)

with open('spamspam.txt', 'w', opener=opener) as f:
    print('This will be written to somedir/spamspam.txt', file=f)

os.close(dir_fd) # don't leak a file descriptor

#open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)